package cn.b.training.baby_education_training.api;

import cn.b.training.baby_education_training.entity.BabyHeight;
import cn.b.training.baby_education_training.entity.BabyInfo;
import cn.b.training.baby_education_training.entity.BabyWeight;
import cn.b.training.baby_education_training.entity.UserInfo;
import cn.b.training.baby_education_training.repository.BabyHeightRepository;
import cn.b.training.baby_education_training.repository.BabyInfoRepository;
import cn.b.training.baby_education_training.repository.BabyWeightRepository;
import cn.b.training.baby_education_training.service.BabyInfoService;
import cn.b.training.baby_education_training.service.UserInfoService;
import cn.b.training.baby_education_training.service.dto.BabyDto;
import cn.b.training.baby_education_training.service.dto.BabyInfoDto;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-01 08:15
 **/
@RestController
@RequestMapping("/baby")
public class BabyInfoController {

    private final UserInfoService userInfoService;
    private final BabyInfoService babyInfoService;
    private final BabyHeightRepository babyHeightRepository;
    private final BabyWeightRepository babyWeightRepository;
    private final BabyInfoRepository babyInfoRepository;

    public BabyInfoController(UserInfoService userInfoService, BabyInfoService babyInfoService, BabyHeightRepository babyHeightRepository, BabyWeightRepository babyWeightRepository, BabyInfoRepository babyInfoRepository) {
        this.userInfoService = userInfoService;
        this.babyInfoService = babyInfoService;
        this.babyHeightRepository = babyHeightRepository;
        this.babyWeightRepository = babyWeightRepository;
        this.babyInfoRepository = babyInfoRepository;
    }

    @PostMapping("/insert/baby")
    public ResponseEntity insertBaby(@RequestBody BabyDto babyDto){
        try {
            boolean b = babyInfoService.insertBaby(babyDto);
            if (b){
                JSONObject result = new JSONObject();
                result.put("success","添加成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","添加失败");
                return ResponseEntity.status(400).body(error);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @PutMapping("/update/baby")
    public ResponseEntity updateBaby(@RequestBody BabyDto babyDto){
        try {
            boolean b = babyInfoService.updateBabyInfo(babyDto);
            if (b){
                JSONObject result = new JSONObject();
                result.put("success","修改成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","修改失败");
                return ResponseEntity.status(400).body(error);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }




    @DeleteMapping("/delete/baby")
    public ResponseEntity deleteBaby(@RequestParam Long id){
        try {
            boolean b = babyInfoService.deleteBaby(id);
            if (b){
                JSONObject result = new JSONObject();
                result.put("success","删除成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","删除失败");
                return ResponseEntity.status(400).body(error);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping
    public ResponseEntity getBabyByUserId() {
        try {
            Authentication user = SecurityContextHolder.getContext().getAuthentication();
            User currentUser = (User) user.getPrincipal();
            UserInfo userInfo = this.userInfoService.getUser(currentUser.getUsername());
            List<BabyInfo> babyInfos = this.babyInfoService.getBabyByUserId(userInfo.getId());
            return ResponseEntity.ok(babyInfos);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error", e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping("/babyInfo")
    public ResponseEntity getBabyById(@RequestParam Long id){
        try{
            Authentication user= SecurityContextHolder.getContext().getAuthentication();
            User currentUser=(User)user.getPrincipal();
            UserInfo userInfo=this.userInfoService.getUser(currentUser.getUsername());
            if(userInfo!=null){
                BabyInfo babyInfo = this.babyInfoService.getBabyById(id);
                List<BabyHeight> babyHeight = this.babyHeightRepository.findHeightByBabyId(babyInfo.getId());
                List<BabyWeight> babyWeight = this.babyWeightRepository.findWeightByBabyId(babyInfo.getId());
                BabyInfoDto babyDto = new BabyInfoDto();
                babyDto.setId(babyInfo.getId());
                babyDto.setBabyName(babyInfo.getBabyName());
                babyDto.setBabyGender(babyInfo.getBabyGender());

                babyDto.setBabyBirth(babyInfo.getBabyBirth());
                babyDto.setBabyAvatar(babyInfo.getBabyAvatar());

                babyDto.setBabyHeight(babyHeight);
                babyDto.setBabyWeight(babyWeight);
                return ResponseEntity.ok(babyDto);
            }else{
                JSONObject error = new JSONObject();
                error.put("error","登录身份不合法");
                return ResponseEntity.status(400).body(error);
            }
        }catch(Exception e){
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    //@GetMapping("/babyInfo")
    //public ResponseEntity getBaby(Long id){
    //    try{
    //        Authentication user= SecurityContextHolder.getContext().getAuthentication();
    //        User currentUser=(User)user.getPrincipal();
    //        UserInfo userInfo=this.userInfoService.getUser(currentUser.getUsername());
    //        if(userInfo!=null){
    //            BabyInfo babyInfo = this.babyInfoService.getBabyById(id);
    //            return ResponseEntity.ok(babyInfo);
    //        }else{
    //            JSONObject error = new JSONObject();
    //            error.put("error","登录身份不合法");
    //            return ResponseEntity.status(400).body(error);
    //        }
    //    }catch(Exception e){
    //        JSONObject error = new JSONObject();
    //        error.put("error",e.getMessage());
    //        return ResponseEntity.status(400).body(error);
    //    }
    //}
}
